<script type="text/javascript" src="<?=base_url()?>resource/js/layer/layer.js"></script>
<script type="text/javascript" src="<?=base_url()?>resource/js/layer/extend/layer.ext.js"></script>
<script type="text/javascript">
    function printHtml(html) {
        var bodyHtml = document.body.innerHTML;
        document.body.innerHTML = "<img width='1089px' height='769px' src='"+html+"'>";
        window.print();
        document.body.innerHTML = bodyHtml;
        layer.closeAll(); //疯狂模式，关闭所有层
    }
    function PrintPackage(img){
        layer.open({
            type: 1,
            title:'包裹信息',
            skin: 'layer-oms-skin',
            area: ['1050px', '650px'], //宽高
            content: "<img  src='"+img+"' width='1000px' height='600px'><input  type='button' value='打印' onclick=\"printHtml(\'"+img+"\')\">"
        });
    }
    /**
     * 拆包
     */
    function unpack(){
        var ck= $("input[name='pid[]']").is(':checked');
        var pid='';
        var package_id=$('#bgid').val();
        if(ck) {
            if (confirm('你确定拆分包裹吗')) {
                $("input:checkbox[name='pid[]']:checked").each(function(i){
                    if(0==i){
                        pid = $(this).val();
                    }else{
                        pid += (","+$(this).val());
                    }
                });
                $.post("<?=blang_url('ajax/ajax/UnPack')?>",{'package_id':package_id,'pid':pid},function(data){
                    if(data==1){
                        alert('拆分成功！');
                        location.href='<?=blang_url('orders/package/')?>';
                    }else{
                        alert('拆分失败！系统错误！');
                    }
                });
            }
        }else{
            layer.tips('请先选择产品', '#pselect', {
                tips: [1, '#0FA6D8'] //还可配置颜色
            });
            return false;
        }
    }
    function View(id){
        $('#packageInfo .packageDetail').empty();
        $('.plist').remove();
        $('#packageInfo').attr('display','block');
        layer.open({
            type: 1,
            skin: 'layer-oms-skin',
            title:'包裹详细',
            area: ['600px', '480px'], //宽高
            content: $('#packageInfo')
        });
        $.post("<?=blang_url('ajax/ajax/PackageInfo')?>",{'id':id},function(data){
            var data=JSON.parse(data);
            $('#bgh').html("<input type='hidden' id='bgid' value='"+id+"'>"+data.packageData.package_id);
            $('#ddh').html(data.packageData.order_id);
            $('#jjr').html(data.packageData.noter);
            $('#hyfs').html(data.packageData.delivery_method);
            $('#zzh').html(data.packageData.trace_id);
            $('#bgzl').html(data.packageData.freight);
            $('#yf').html(data.packageData.freight);
            $('#shr').html(data.packageData.consignee);
            $('#fhrq').html(data.packageData.time);
            $('#jd').html(data.packageData.RECEIPT);
            $('#cs').html(data.packageData.city);
            $('#sf').html(data.packageData.state);
            $('#gj').html(data.packageData.country);
            $('#yb').html(data.packageData.zipcode);
            var productData=JSON.parse(data.productData);
            var trHTML='';
            //兼容新旧两套数据
            for(var i=0;i<productData.length;i++){
                if(productData[i].product_id) {
                    trHTML = "<tr id='" + productData[i].product_id + "' class='plist'>" +
                    "<td class='text-left'><input type='checkbox' name='pid[]' value='" + productData[i].product_id + "'></td>" +
                    "<td class='text-left'>" + productData[i].sku + "</td>" +
                    "<td class='text-left'>" + productData[i].product_name + "</td>" +
                    "<td class='text-right'>" + productData[i].brand_name + "</td>" +
                    "<td class='text-right'>" + productData[i].product_model + "</td>" +
                    "<td class='text-right'>" + productData[i].product_quantity + "</td>" +
                    "</tr>";
                }else{
                    trHTML = "<tr id='" + productData[i].id + "' class='plist'>" +
                    "<td class='text-left'><input type='checkbox' name='pid[]' value='" + productData[i].id + "'></td>" +
                    "<td class='text-left'>" + productData[i].productSku + "</td>" +
                    "<td class='text-left'>" + productData[i].sort_name_sc + "</td>" +
                    "<td class='text-right'>" + productData[i].product_brand + "</td>" +
                    "<td class='text-right'>" + productData[i].product_model + "</td>" +
                    "<td class='text-right'>" + productData[i].quantity + "</td>" +
                    "</tr>";
                }
                $('#packageTable tbody').append(trHTML);
            }
        });
    }
    function SendNacex(){
        var Nck= $("input[class='Nacex']").is(':checked');
        var Dck= $("input[class='DHL']").is(':checked');
        if(Nck && !Dck) {
            layer.msg('正在拼命为你发送....', {icon: 16, time: 20000});
            return true;
        }else{
            layer.tips('请先选择包裹', '#pkselect', {
                tips: [1, '#0FA6D8'] //还可配置颜色
            });
            return false;
        }
    }
    function SendDHL(){
        $('#LogisticsForm').attr('action','<?=blang_url('orders/package/sendDHL'.$str)?>');
        var Dck= $("input[class='DHL']").is(':checked');
        var Nck= $("input[class='Nacex']").is(':checked');
        if(Dck && !Nck) {
            layer.msg('正在拼命为你发送....', {icon: 16, time: 20000});
            return true;
        }else{
            layer.tips('请先选择包裹', '#pkselect', {
                tips: [1, '#0FA6D8'] //还可配置颜色
            });
            return false;
        }
    }
</script>
<!--菜单 start-->
<?php $this->load->view('defaults/common/left');?>
<!--菜单 end-->
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
        <h1><?=lang('pk_bglb')?></h1>
        <?=$this->breadcrumb->generateHTML();?>
        </div>
    </div>
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title"><i class="fa fa-list"></i><?=lang('pk_bglb')?></h3>
            </div>
            <div class="panel-body">
                <div class="well" style="padding:0 19px 19px;">
                    <div class="row">
                        <form action="<?=blang_url('orders/package/GetPackageData')?>" method="post" name="searchform" >
                            <div class="col-sm-4">
                                <div class="form-group">
                                    <label class="control-label"><?=lang('pk_cxtj')?></label>
                                    <input class="form-control" name="keyword" value="<?=$keyword?>"  placeholder="<?=lang('pk_pls')?>" size="60" />
                                </div>
                            </div>
                            <div class="col-sm-4">
                                <div class="form-group">
                                    <label class="control-label">TYPE</label>
                                    <select class="form-control" name="type">
                                        <option value="">-All-</option>
                                        <option value="Nacex" <?=$type=='Nacex'?'selected':''?>>-Nacex-</option>
                                        <option value="DHL" <?=$type=='DHL'?'selected':''?>>-DHL-</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-sm-4">
                                <div class="form-group">
                                    <label class="control-label">Country</label>
                                    <select class="form-control" name="country">
                                        <option value="">-All-</option>
                                        <?php foreach($countryList as $row){?>
                                            <option value="<?=$row['country']?>" <?=$row['country']==$country?'selected':''?> ><?=$row['country']?></option>
                                        <?php }?>
                                    </select>
                                </div>
                                <input type="submit" class="btn btn-primary pull-right" name="searchSubmit" value="<?=lang('public_cx')?> " />
                            </div>
                        </form>
                    </div>
                </div>
                <div class="table-responsive">
                    <form action="<?=blang_url('orders/package/PostNacex')?>" id="LogisticsForm" method="get">
                        <table class="table table-bordered table-hover oms-table" cellspacing="0">
                            <thead>
                                <tr>
                                    <th class="text-left" id="pkselect">select</th>
                                    <th class="text-left"><?=lang('pk_bgh')?></th>
                                    <th class="text-left"><?=lang('pk_ddh')?></th>
                                    <th class="text-left"><?=lang('pk_hyfs')?></th>
                                    <th class="text-right"><?=lang('pk_zzh')?></th>
                                    <th class="text-right"><?=lang('pk_tjr')?></th>
                                    <th class="text-right"><?=lang('pk_scsj')?></th>
                                    <th class="text-right"><?=lang('public_cz')?></th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php foreach($packageList as $row){?>
                                <tr style="color:
                                <?php switch($row['is_package']){
                                    case 1:
                                        echo '#00CC00';
                                        break;
                                    case 2:
                                        echo '#ff0000';
                                        break;
                                    default:
                                        echo '#666';
                                }?>">
                                    <td class="text-left">
                                        <input type="hidden" name="keyword" value="<?=$keyword?>">
                                        <input type="checkbox" id="pkId[]" name="pkId[]" class="<?=$row['delivery_method']=='DHL'?'DHL':'Nacex'?>" <?=$row['is_package']==1 || !in_array($row['delivery_method'],array('Nacex','DHL'))?'disabled':''?> value="<?=$row['id']?>">
                                    </td>
                                    <td class="text-left"><?=$row['package_id']?></td>
                                    <td class="text-left">
                                        <a href="<?=blang_url('orders/Order_new/?keywordOrder='.$row['order_id'])?>" ><?=$row['order_id']?></a>
                                    </td>
                                    <td class="text-left"><?=$row['delivery_method']?></td>
                                    <td class="text-right"><?=$row['trace_id']?></td>
                                    <td class="text-right"><?=$row['noter']?></td>
                                    <td class="text-right"><?=$row['time']?date('Y-m-d H:i:s',$row['time']):''?></td>
                                    <td class="text-right">
                                        <a class="btn btn-primary btn-sm" href="javascript:void(0)" onclick="View(<?=$row['id']?>)">detail</a>
                                        <?php if($row['trace_id']): ?>
                                        <a class="btn btn-oms btn-sm" href="javascript:void(0)" onclick="PrintPackage('<?=$row['documents']?>')">print</a>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            <?php }?>
                            <tr>
                                <td colspan="8" style="text-align:right;">
                                    <input type="submit" class="btn btn-oms btn-sm" onclick="return SendNacex()"  value="send parcel information to Nacex">
                                    <input type="submit" class="btn btn-oms btn-sm" onclick="return SendDHL()"  value="send parcel information to DHL">
                                </td>
                            </tr>
                            </tbody>
                        </table>
                        <?php if($show_pages):?>
                        <div class="page">
                            <?=$show_pages?>
                        </div>
                        <?php endif;?>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <div id="packageInfo" style="display: none;padding:20px;">
        <table class="style20170608" cellspacing="0" style="margin-bottom:10px;">
            <tbody>
            <tr>
                <td class="tableSubtitle odd">包裹号</td>
                <td id="bgh" class="tableContent packageDetail"></td>
                <td class="tableSubtitle odd">订单号</td>
                <td id="ddh" class="tableContent packageDetail"></td>                                                               
            </tr>
            <tr>
                <td class="tableSubtitle even">寄件人</td>
                <td id="jjr" class="tableContent packageDetail"></td>
                <td class="tableSubtitle even">订单货运方式</td>
                <td id="hyfs" class="tableContent packageDetail"></td>                                                               
            </tr>
             <tr>
                <td class="tableSubtitle odd">收件人</td>
                <td id="shr" class="tableContent packageDetail" colspan="3"></td>                                                            
            </tr>
            <tr>
                <td class="tableSubtitle even">追踪号</td>
                <td id="zzh" class="tableContent packageDetail"></td>
                <td class="tableSubtitle even">包裹重量</td>
                <td id="bgzl" class="tableContent packageDetail"></td>                                                               
            </tr>
            <tr>
                <td class="tableSubtitle odd">运费</td>
                <td id="yf" class="tableContent packageDetail"></td>
                <td class="tableSubtitle odd">发货日期</td>
                <td id="fhrq" class="tableContent packageDetail"></td>                                                               
            </tr>
        </tbody>
        </table>
        <h3 class="panel-title">收货地址</h3>
        <table class="table table-bordered table-hover oms-table">
            <thead>
            <tr>
                <th class="text-left" style="15%;">街道</th>
                <th class="text-left">城市</th>
                <th class="text-left">省份</th>
                <th class="text-right">国家</th>
                <th class="text-right">邮编</th>
            </tr>
            </thead>
            <tbody>
                <tr>
                    <td id="jd" class="text-left"></td>
                    <td id="cs" class="text-left"></td>
                    <td id="sf" class="text-left"></td>
                    <td id="gj" class="text-right"></td>
                    <td id="yb" class="text-right"></td>
                </tr>
            </tbody>
        </table>
        <h3 class="panel-title">包裹产品<a class="btn btn-primary btn-sm pull-right" style="color:#fff;" href="javascript:void(0)" onclick="return unpack()">拆包</a></h3>
        <table id="packageTable" class="table table-bordered table-hover oms-table">
            <thead>
            <tr>
                <th class="text-left" id="pselect">select</th>
                <th class="text-left">sku</th>
                <th class="text-left">类型</th>
                <th class="text-right">品牌</th>
                <th class="text-right">型号</th>
                <th class="text-right">数量</th>
            </tr>
            </thead>
            <tbody>
                
            </tbody>
        </table>
           
    </div>
</div>
